package com.atguigu.wordcount.flowbean;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @ClassName FlowReducer
 * @DeScription FlowReducer
 * @Author 剧情再美终是戏
 * @Date 2019/11/18 17:13
 * @Version 1.0
 **/
public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {

    private FlowBean flowBean = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        long uploadSum = 0;
        long downSum = 0;
        if (null != values) {
            for (FlowBean value : values) {
                uploadSum += value.getUploadSum();
                downSum += value.getDownSum();
            }
        }
        // 组装数据并将结果写出
        flowBean.setUploadSum(uploadSum);
        flowBean.setDownSum(downSum);
        flowBean.setTotal(downSum + uploadSum);
        context.write(key, flowBean);
    }
}
